/*************************************************
 *FileName:      personal.js
 *Description:   个人中心.js
 *Others:        陈灿
 *************************************************/
    /*
     * 启动
     */
    mui.init();
    mui('.mui-scroll-wrapper').scroll({
        indicators: false,      //是否显示滚动条 默认为true
        deceleration: 0.003,    //阻尼系数,系数越小滑动越灵敏 默认0.0006
        bounce: false           //是否启用回弹 默认true
    });
    /*
     * 变量初始化
     */
    var friend_uid = getUrlParam('friend_uid');
    var chat = getUrlParam('chat');
    var uid = localStorage.replace_uid;
    var token = localStorage.replace_token;
    var friend_nickname = '';
    var success_date_nums = 0;
    var date_nums = 0;
    var good_nums = 0;
    var bad_nums = 0;
    var complaint_nums = 0;
    var my_date_cards = 0;
    var my_friend_cards = 0;
    var date_cost = 0;
    var my_vip = 0;
    var my_verify = 0,other_verify;
    var add_friend_cost = 0;
    var gallery = mui('.mui-slider'),oshedate_status;
    var personal_oppo=true;
    var is_ios = common.browserType().ios; 
    /*
     * 主程序
     */ 
    $(document).ready(function() 
    {
        //样式加载
        pagecss();
    });
    mui.init({
        beforeback: function() {
            localStorage.guideurl = null;
        }
    });
    
    mui.plusReady(function(){ 
        var wv=plus.webview.currentWebview();// 关闭侧滑返回功能
        wv.setStyle({'popGesture':'none'});
        var old_back = mui.back;
        mui.back = function() {
            if(personal_oppo==true){
                old_back();
            }else{
                personal_oppo=true;
                $('.img-look').removeClass('active');
                $('#media')[0].pause();
            }
        }
    });
    
    /**
     * pagecss
     * 样式加载
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800 
     * @param    int        
     * @return   void
     */
    function pagecss(){
        mui.plusReady(getFriendInfo)
        mui.plusReady(getFriendsImages)
//      getFriendsImages();
//      getFriendInfo();
        var p =null;
        var oarr=common.browserType();
        $('.personal').on('tap','.con-voice-img',function(){
            var vic = $(this).attr('data-voice');
            if(oarr.ios){ 
                if(p != null){
                    p.stop();
                }
                var dtask = plus.downloader.createDownload(vic, {}, function ( d, status ) {
                // 下载完成
                    if ( status == 200 ) { 
                        if ( plus.audio == undefined ) {
                            alert( "Device not ready!" );
                        }
                        p = plus.audio.createPlayer(d.filename);
                        p.play( function () {
                            
                            p = null;
                        }, function ( e ) {
                           plus.nativeUI.toast("播放错误: " + e.message);
                        });
                        
                    } else {
                        alert( "网络错误"); 
                    }  
                });
                dtask.start();                  
            }else{
                common.playAudio(vic);
            }
        })//语音播放
        
        $('.personal').on('tap','.footer-addfriend',function(){
            if(!other_verify){
                mui.toast('对方未完成个人认证',{ duration:2000, type:'div' });
                return false;
            }
            if(!my_vip){
                mui.toast('只有vip才能进行好友申请',{ duration:2000, type:'div' });
                return false;
            }
            if(!my_verify){
                mui.toast('必须完成个人认证才能进行好友申请',{ duration:2000, type:'div' });
                return false;
            }
            
            var em = ''
            var method = '';
            var confirm = '';
            if(my_friend_cards){
                em = '您有好友卡 <em class="gift-hyk"></em> ×'+my_friend_cards+'，<br/>是否使用？';
                method = 'cards'
                confirm = '使用';
            }else{
                em = '您将添加'+friend_nickname+'为好友，<br/>需支付换币： <em class="gift-jb"></em> ×'+add_friend_cost;
                method = 'balance';
                confirm = '支付';
            }
            if(chat){
                var odata={ uid: uid, token: token, friend_uid: friend_uid, method: method ,introducer_uid:chat}
            }else{
                var odata={ uid: uid, token: token, friend_uid: friend_uid, method: method }
            }
            mui.confirm(em,'提示',[confirm,'取消'],function(e){
                if(e.index == 0){
                    common.loadingIn();
                    $.ajax({
                        type: "post",
                        url: DOMAIN_NAME + "/index.php/api/friends_apply/ApplyAdd",
                        dataType:'jsonp',
                        data: odata,
                        success: function(data) {
                            common.loadingOut();
                            if(data.error_code == 0){
                                my_date_cards = data.my_date_cards;
                                mui.toast('好友申请已发送，等待对方确认！',{ duration:2000, type:'div' });
                            }else if(data.error_code == 1001){
                                setTimeout(function(){
                                    exitlogin();
                                },1000);
                            }else{
                                setTimeout(function(){
                                    mui.confirm(data.error_msg,'提示',['确定','取消'],function(e2){
                                    },'div');
                                },300)
                            }
                        }
                    });
                }
                    
            },'div');
                
        })//个人首页添加朋友
        
        $('.personal').on('tap','.footer-love',function(){
            if(oshedate_status!=3){
                mui.toast('对方还未通过约会认证',{ duration:2000, type:'div' });
                return false;
            }
            mui.confirm('<span class="yuyue-color">成功约会：<em class="yh">'+success_date_nums+'次</em></span><span class="yuyue-color">收到的约会：<em class="yh">'+date_nums+'次</em></span><span class="yuyue-color">被赞次数：<em class="yh">'+good_nums+'次</em></span><span class="yuyue-color">被差评次数：<em class="yh">'+bad_nums+'次</em></span><span class="yuyue-color">被投诉次数：<em class="yh">'+complaint_nums+'次</em></span><span class="yuyue-color"></span>','约会信用',['约会','取消'],function(e){
                if(e.index == 0){
                    var em = ''
                    var method = '';
                    if(my_date_cards >= date_cost){
                        em = '需支付约会卡：<em class="gift-yhk"></em>×'+date_cost;
                        method = 'cards';
                    }else{
                        em = '需支付换币：<em class="gift-jb"></em>×'+date_cost;
                        method = 'balance';
                    }
                    mui.confirm('您将对'+friend_nickname+'发起约会，<br />'+em,'发起约会',['支付','取消'],function(e1){
                        if(e1.index == 0){
                            common.loadingIn();
                            $.ajax({
                                type: "post",
                                url: DOMAIN_NAME + "/index.php/api/users_date/dateAdd",
                                dataType:'jsonp',
                                data: { uid: uid, token: token, friend_uid: friend_uid, method: method },
                                success: function(data) {
                                    common.loadingOut();
                                    if(data.error_code == 0){
                                        my_date_cards = data.my_date_cards;
                                        mui.toast('约会申请已发送，等待对方确认！',{ duration:2000, type:'div' });
                                    }else if(data.error_code == 1001){
                                        setTimeout(function(){
                                            exitlogin();
                                        },1000);
                                    }else{
                                        if(data.error_msg=='余额不足'){
                                            mui.confirm(data.error_msg,'提示',['充值','取消'],function(e2){
                                                if(e2.index == 0){
                                                    common.pageTransition('recharge.html');
                                                }
                                            },'div');
                                        }else{
                                            mui.confirm(data.error_msg,'提示',['确定','取消'],function(e2){
                                            },'div');
                                        }
                                    }
                                }
                            });
                        }
                    },'div');
                }
            },'div');
        })//个人首页预约
        
        $('.header').on('tap','.collection',function(){
            var obj = $(this);
            if($(this).hasClass('active')){
                $.ajax({
                    type: "post",
                    url: DOMAIN_NAME + "/index.php/api/users_attention/attentionCancel",
                    dataType:'jsonp',
                    data: { uid: uid, token: token, friend_uid: friend_uid },
                    success: function(data) {
                        if(data.error_code == 0){
                            obj.removeClass('active');
                            mui.toast('取消关注成功！',{ duration:2000, type:'div' });
                        }else if(data.error_code == 1001){
                            setTimeout(function(){
                        exitlogin();
                    },1000);
                        }else{
                            mui.toast('取消关注失败！',{ duration:2000, type:'div' });
                        }
                    }
                });
            }else{
                $.ajax({
                    type: "post",
                    url: DOMAIN_NAME + "/index.php/api/users_attention/attentionAdd",
                    dataType:'jsonp',
                    data: { uid: uid, token: token, friend_uid: friend_uid },
                    success: function(data) {
                        if(data.error_code == 0){
                            obj.addClass('active');
                            mui.toast('关注成功！',{ duration:2000, type:'div' });
                        }else if(data.error_code == 1001){
                            setTimeout(function(){
                        exitlogin();
                    },1000);
                        }else{
                            mui.toast('关注失败！',{ duration:2000, type:'div' });
                        }
                    }
                });
            }
        })
        
        $('.personal a.con-gift-a').on('tap',function(){
            if($(this).parent().nextAll().is(':hidden')){
                $(this).removeClass('mui-icon-arrowdown').addClass('mui-icon-arrowup') ;
                $(this).parent().nextAll().show();
            }else{
                $(this).removeClass('mui-icon-arrowup').addClass('mui-icon-arrowdown');
                $(this).parent().nextAll().hide();
            }
        })//礼物下拉框
        
        $('.personal .complain').on('tap',function(){
            $('.popover').fadeIn();
            $('.table-view2').fadeIn();
        })//个人首页投诉
        
        $('.table-view-cell-cancel').on('tap',function(){
            $('.popover').css({display:'none'});
            $('.table-view2').fadeOut();
            if($(this).hasClass('sure')){
                var report_content = [];
                var i = 0;
                $('.table-select').each(function(){
                    if($(this).hasClass('active')){
                        report_content[i] = $(this).text();
                        i++;
                    }
                })
                if(!report_content){
                    mui.toast('请选择您要举报的内容！',{ duration:2000, type:'div' });
                    return false;
                }
                $.ajax({
                    type: "post",
                    url: DOMAIN_NAME + "/index.php/api/report/reportAdd",
                    dataType:'jsonp',
                    data: { uid: uid, token: token, report_uid: friend_uid, report_content: report_content, type: 2 },
                    success: function(data) {
                        if(data.error_code == 0){
                            mui.toast('举报成功！',{ duration:2000, type:'div' });
                        }else if(data.error_code == 1001){
                            setTimeout(function(){
                        exitlogin();
                    },1000);
                        }else{
                            mui.toast('举报失败！',{ duration:2000, type:'div' });
                        }
                    }
                });

            }
        })//下拉菜单栏取消
        
        $('.personal').on('tap','.table-view2 .table-view-cell-a',function(){
            $(this).toggleClass('active');
        })
        
        $('.personal').on('tap','.table-view2 .table-view-cell-cancel',function(){
            $('.table-view2 .table-view-cell-a').removeClass('active');
        })
        
        $('.con-friends').on('tap',function(){
            common.pageTransition('myfriends.html?this=other&friend_uid='+friend_uid)
        })
        
        $('.con-autograph').on('tap',function(){
            $('.table-view1').fadeIn();
            $('.popover').css({display:'block'});
        })
        
        $('.table-view-cell-cancel').on('tap',function(){
            $('.popover').css({display:'none'});
            $('.table-view1').fadeOut();
        })
    
        $('.table-view-cell-affirm').on('tap',function(){
            var remark_nickname = $("textarea[name='remark_nickname']").val();
            var remark = $("input[name='remark']").val();
            if(remark_nickname == remark){
                $('.popover').css({display:'none'});
                $('.table-view1').fadeOut();
                return;
            }
            $.ajax({
                type: "post",
                url: DOMAIN_NAME + "/index.php/api/user/remarkAdd",
                dataType:'jsonp',
                data: { uid: uid, token: token, remark_uid: friend_uid, remark_nickname: remark_nickname },
                success: function(data) {
                    if(data.error_code == 0){
                        $('.popover').css({display:'none'});
                        $('.table-view1').fadeOut();
                        $('.con-autograph').val(remark_nickname);
                        $('.remark_nickname').val(remark_nickname);
                        mui.toast('备注成功！',{ duration:2000, type:'div' });
                    }else if(data.error_code == 1001){
                        setTimeout(function(){
                        exitlogin();
                    },1000);
                    }else{
                        mui.toast('备注失败！',{ duration:2000, type:'div' });
                    }
                }
            });

        })
    }

    function getFriendInfo(){
        $.ajax({
            type: "post",
            url: DOMAIN_NAME + "/index.php/api/user/getFriendInfo",
            dataType:'jsonp',
            data: { uid: uid, token: token, friend_uid: friend_uid },
            success: function(data) {
                if(data.error_code == 0){
                    user = data.data;
                    success_date_nums = user.success_date_nums;
                    date_nums = user.date_nums;
                    good_nums = user.good_nums;
                    bad_nums = user.bad_nums;
                    complaint_nums = user.complaint_nums;
                    friend_nickname = user.nickname;
                    my_date_cards = user.my_date_cards;
                    my_friend_cards = user.my_friend_cards;
                    date_cost = user.date_cost;
                    my_vip = user.my_vip;
                    my_verify = user.my_verify;
                    add_friend_cost = user.add_friend_cost;
                    oshedate_status=user.date_status;
                    var same_num = '';
                    $('.nameid').html('YP: '+user.uid);
                    $('.name').html(user.nickname);
                    $('.msg-vip').html(user.level+'级魅力');
                    var sex = '<em class="'+user.sex+'"></em>'+user.age+'岁'
                    $('.msg-year').html(sex);
                    if(user.personal_status == 3){
                        other_verify=true;
                        $('.msg-renzheng').html('已认证');
                    }else{
                        other_verify=false;
                        $('.msg-renzheng').html('未认证');
                    }
                    
                    if(my_vip&&my_verify&&user.personal_status == 3){
                        $('.footer-addfriend').addClass('active')
                    }
                    
                    if(user.date_status == 3){
                        $('.footer-love').addClass('active');
                    }
                    
                    $('.height em').html(parseInt(user.height)+'cm');
                    $('.weight em').html(parseInt(user.weight)+'kg');
                    $('.fans em').html(user.fans_num);
                    if(user.verify_video){
                        $('.con-video').html('<img class="con-img"  src="'+user.verify_video.thumb+'"/>')
                        $('.img-look-box').html('<div id="vdo-loding"></div><video class="active" id="media" src="'+user.verify_video.video+'"></video>')
                        $('#media')[0].addEventListener('canplay',function(){
                            $('#vdo-loding').remove();
                            $('#media').removeClass('active');
                        })
                    }else{
                        $('.con-video').remove();
                    }
                    if(user.voice_signature){
                        $('.con-voice').html('<img class="con-voice-img" src="images/voice.png" data-voice="'+DOMAIN_NAME+user.voice_signature+'"/>');
                    }
                    var p =null;
        $('.particulars').on('tap','.particulars-qianm',function(){
            var vic = $(this).attr('data-src');
            if(oarr.ios){
                if(p != null){
                    p.stop();
                }
                var dtask = plus.downloader.createDownload(vic, {}, function ( d, status ) {
                // 下载完成
                    if ( status == 200 ) { 
                        if ( plus.audio == undefined ) {
                            alert( "Device not ready!" );
                        }
                        p = plus.audio.createPlayer(d.filename);
                        p.play( function () {
                            
                            p = null;
                        }, function ( e ) {
                           plus.nativeUI.toast("播放错误: " + e.message);
                        });
                        
                    } else {
                        alert( "网络错误"); 
                    }  
                });
                dtask.start();                  
            }else{
                common.playAudio(vic);
            }
            
        })//语音播放
                    
                    if(user.remark){
                        $('.con-autograph').val(user.remark);
                        $('.remark_nickname').val(user.remark);
                    }
                    if(user.files){
                        var li = '';
                        for(var i=user.files.length-1; i>=0; i--){
                            if(user.files[i].type=='2'){
                                li += '<img src="'+user.files[i].thumb+'"/>';
                            }else{
                                li += '<img src="'+DOMAIN_NAME+user.files[i].thumb+'"/>';
                            }
                        }
                        $('.friends-font').after(li);
                    }
                    if(user.tags){
                        var tags = '';
                        for(var i=0; i<user.tags.length; i++){
                            var a = user.tags[i].name?user.tags[i].name:user.tags[i].content
                            tags += '<a>'+a+'</a>';
                        }
                        $('.con-biaoqian').html(tags);
                    }
                    if(user.same_interest){
                        same_num = user.same_interest.length;
                        var same_interest = '';
                        for(var i=0; i<user.same_interest.length; i++){
                            var b = user.same_interest[i].name?user.same_interest[i].name:user.same_interest[i].content
                            same_interest += '<a>'+b+'</a>';
                        }
                        $('.same-interest').html(same_interest);
                    }else{
                        same_num = 0;
                    }
                    $('.con-lick').eq(0).html(same_num+'个共同兴趣')
                    if(user.interest){
                        var same_num = user.interest.length;
                        for(var i=0; i<user.interest.length; i++){
                            var c = user.interest[i].name?user.interest[i].name:user.interest[i].content
                            $('.con-xingqu').eq(user.interest[i].type-3).append('<a>'+c+'</a>').css('display','block');
                        }
                        $('.same-interest').html(same_interest);
                    }
                    if(user.industry){
                        $('.industry').html(user.industry.name);
                    }
                    if(user.work_field){
                        $('.work_field').html(user.work_field.name);
                    }
                    if(user.gift){
                        var gift = '';
                        for(var i=0; i<user.gift.length; i++){
                            gift += '<a class="gift"><span class="giftimg-div"><img class="giftimg" src="'+DOMAIN_NAME+user.gift[i].url+'"/></span><span class="text">'+user.gift[i].name+' ×'+user.gift[i].sum+'</span><em>'+parseInt(user.gift[i].money)+'换币</em></a>'
                        }
                        $('.gift-list').html(gift);
                    }
                    if(user.piece){
                        var piece = '';
                        for(var i=0; i<user.piece.length; i++){
                            piece += '<a class="gift"><span class="giftimg-div"><img class="giftimg" src="'+DOMAIN_NAME+user.piece[i].url+'"/></span><span class="text">'+user.piece[i].name+' ×'+user.piece[i].sum+'</span><em>'+parseInt(user.piece[i].money)+'换币</em></a>'
                        }
                        $('.pieces-list').html(piece);
                    }
                    if(user.attention){
                        $('.collection').addClass('active')
                    }
                    $('.company').html(user.company);
                    $('.home').html(user.home);
                    $('.place').html(user.place);
                }else if(data.error_code == 1001){
                    setTimeout(function(){
                        exitlogin();
                    },1000);
                }else{
                    mui.toast(data.error_msg,{ duration:2000, type:'div' });
                }
            }
        }); 
    }


    function getFriendsImages(){
        $.ajax({
            type: "post",
            url: DOMAIN_NAME + "/index.php/api/user/getFriendsImages",
            dataType:'jsonp',
            data: { uid: uid, token: token, friend_uid: friend_uid },
            success: function(data) {
                if(data.error_code == 0){
                    var imgs = '';
                    for(var i=0; i<data.data.length; i++){
                        imgs += '<div class="mui-slider-item"><a ><img class="lazy imgbox" src="" data-lazy="'+DOMAIN_NAME+data.data[i].url+'" /></a></div>'
                    }
                    $('.mui-slider-group').html(imgs)
                    mui(".mui-slider").slider({});
                    $('.img-sub').html('<i>1</i>/'+data.data.length+'');
                    document.querySelector('.mui-slider').addEventListener('slide', function(event) {
                      //注意slideNumber是从0开始的；
                        $('.img-sub i').text(event.detail.slideNumber+1);
                    });
                }else{
                    var imgs = '<div class="mui-slider-item"><a ><img src="images/personal_img.jpg" /></a></div>'
                    $('.mui-slider-group').html(imgs)
                    mui(".mui-slider").slider({interval: 0});
                    $('.img-sub').html('<i>1</i>/1');
                    document.querySelector('.mui-slider').addEventListener('slide', function(event) {
                      //注意slideNumber是从0开始的；
                        $('.img-sub i').text(event.detail.slideNumber+1);
                    });
                }
                
                setTimeout(function(){
                    for(var i = 0 ;i<$('.mui-slider-item').length;  i++){
                        var othis = $('.main .mui-slider-item').eq(i);
                        var othisimg = $('.main .mui-slider-item').eq(i).find('.imgbox');
                        var osrc = $('.mui-slider-item').eq(i).find('.lazy').attr('data-lazy');
                        othis.find('.lazy').attr('src',osrc).removeClass('lazy');
                    }
                },350)
                $('.imgbox').on('load',function(){
                    if($(this).width()>$(this).height()){
                        $(this).addClass('imgbox-h');
                    }
                })//判断宽高
            }
        });
    }
    
    
    
    
    //查看视频
    $('.mui-content').on('tap','.con-video',function(){
        if(!my_vip){
            mui.toast('只有vip才能查看认证视频',{ duration:2000, type:'div' });
            return false;
        }else{
            if(is_ios){
                $('#media')[0].play(); //播放
                $('#media')[0].addEventListener('canplay',function(){//监听是否加载完毕
                    common.loadingOut(); 
                })
            }else{
                personal_oppo=false;
                $('.img-look').addClass('active');
                $('#media')[0].play(); //播放
            }
            
        }
    })
    $('.img-look').on('tap',function(){//点击遮罩隐藏
        $(this).removeClass('active');
        personal_oppo=true;
        
            $('#media')[0].pause(); //暂停
    })
    /**
     * adds //方法名
     * @陈灿 //作者名
     * @DateTime 2018-01-010T0116:25:00+0800 //时间
     * @param    int    $goods_id //参数
     * @param    array  $data //参数
     * @return    array  [error_code, error_msg, goods_id] //返回值
     */